/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.dao.AccountDAO;
import model.dao.RoleDAO;
import model.dao.UserProfileDAO;
import model.enetities.Account;
import model.enetities.Role;
import model.enetities.UserProfile;

/**
 *
 * @author THANMINH
 */
public class Registration extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet Registration</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("</body>");
            out.println("</html>");
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String username = request.getParameter("username");  
        System.out.println(username);
        AccountDAO account = new AccountDAO();
        boolean checkuser = account.onlyAccount(username);
        if(checkuser == false){
           Account acc = new Account();
           //String name = request.getParameter("username");
           acc.setUserName(username);
           acc.setPassword(request.getParameter("password"));
           Role r = RoleDAO.getInstance().getRoleByID(2);
           acc.setIdRole(r);
           acc.setActived(true);
           
           if(account.createAccount(acc)== true){
          
        acc = account.getAccountByName(username);
        int idaccount = acc.getIdAccount();
           
        UserProfile userfile = null;
        UserProfileDAO users = new UserProfileDAO();
        userfile = new UserProfile();
        
        
       // userfile.setIdUser( Integer.parseInt(request.getParameter("iduser")));
        
        userfile.setFullName(request.getParameter("fullname"));
        userfile.setDateOfBirth(Date.valueOf(request.getParameter("dob")));
        userfile.setGender(request.getParameter("gender")=="M"?true:false );
        userfile.setHomeTown(request.getParameter("hometown"));
        userfile.setSchool(request.getParameter("school"));
        userfile.setMobile(request.getParameter("mobile"));
        userfile.setEmail(request.getParameter("email"));
        userfile.setStrengths(request.getParameter("strength"));
        userfile.setLanguages(request.getParameter("language"));
        userfile.setImage(request.getParameter("image"));
        userfile.setAbout(request.getParameter("about"));
        userfile.setFavoriteQuotations(request.getParameter("favoriteQuotations"));
        userfile.setFacebookLink(request.getParameter("facebookLink"));
        userfile.setActive(true);
        userfile.setIdAccount(idaccount);
        
        users.create(userfile);
           }
        }else{
            System.out.println("sai");
        }
        
        
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
